package cn.cloud9.activiti7.processvariable;

import cn.cloud9.activiti7.var.Evection;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 全局变量
 * @author OnCloud9
 * @description
 * @project activiti7
 * @date 2022年08月20日 下午 10:46
 */
public class ProcessGlobalVariable {
    private ProcessEngine processEngine;
    private RepositoryService repositoryService;
    private RuntimeService runtimeService;
    private TaskService taskService;
    private HistoryService historyService;
    private ManagementService managementService;

    /**
     * 初始化预备资源
     */
    @Before
    public void loadingResource() {
        processEngine = ProcessEngines.getDefaultProcessEngine();
        repositoryService = processEngine.getRepositoryService();
        runtimeService = processEngine.getRuntimeService();
        taskService = processEngine.getTaskService();
        historyService = processEngine.getHistoryService();
        managementService = processEngine.getManagementService();
    }

    @Test
    public void deleteDeploy() {
        repositoryService.deleteDeployment("42501", true);
    }

    @Test
    public void deploymentProcess() {
        final Deployment deploy = repositoryService.createDeployment()
                .name("出差流程申请-全局变量使用")
                .addClasspathResource("./bpmn/evection-global-var.bpmn20.xml")
                .deploy();

        final String id = deploy.getId();
        final String name = deploy.getName();
        System.out.println("id = " + id);
        System.out.println("name = " + name);
    }

    /**
     * 在任务开始时添加流程变量
     */
    @Test
    public void startProcess() {
        final Evection evection = new Evection();
        evection.setDays(4D);
        final Map<String, Object> processVarMap = new HashMap<>();

        // 设置全局变量和流程变量
        // 对象形式的流程变量必须要实现序列化接口
        processVarMap.put("evection", evection);
        processVarMap.put("assignee1", "张三");
        processVarMap.put("assignee2", "李四");
        processVarMap.put("assignee3", "王五");
        processVarMap.put("assignee4", "赵六");

        final ProcessInstance instance = runtimeService
                .startProcessInstanceByKey("evection-global-var", processVarMap);
    }

    /**
     * 检查任务走向
     */
    @Test
    public void checkVariableHasEffect() {
        final TaskQuery taskQuery = taskService.createTaskQuery();

//        final Task task = taskQuery
//                .processDefinitionKey("evection-global-var")
//                .processInstanceId("47501")
//                .taskAssignee("张三")
//                .singleResult();

        final Task task = taskQuery
                .processDefinitionKey("evection-global-var")
                .processInstanceId("47501")
                .taskAssignee("李四")
                .singleResult();

        taskService.complete(task.getId());
    }

    /**
     * 在任务执行的时候变化变量
     */
    @Test
    public void setProcessVariableInCompleteTask() {
        final TaskQuery taskQuery = taskService.createTaskQuery();
        final Task task = taskQuery
                .processDefinitionKey("evection-global-var")
                .processInstanceId("47501")
                .taskAssignee("李四")
                .singleResult();

        final Evection evection = new Evection();
        evection.setDays(2D);
        final Map<String, Object> processVarMap = new HashMap<>();
        processVarMap.put("evection", evection);

        taskService.complete(task.getId(), processVarMap);
    }

    /**
     * 在流程实例配置变量
     */
    @Test
    public void setProcessVariableInProcessInstance() {
        final Evection evection = new Evection();
        evection.setDays(2D);
        final Map<String, Object> processVarMap = new HashMap<>();
        processVarMap.put("evection", evection);

        String executionId = "当前流程实例执行ID";

        // 在运行服务对象中指定具体任务的变量, 设置单个值和多个值
        runtimeService.setVariable(executionId, "evection", evection);
        runtimeService.setVariables(executionId, processVarMap);
    }

    /**
     * 在流程实例下具体某个任务中配置变量
     */
    @Test
    public void setProcessVariableInTask() {
        final Evection evection = new Evection();
        evection.setDays(2D);
        final Map<String, Object> processVarMap = new HashMap<>();
        processVarMap.put("evection", evection);

        String taskId = "当前任务执行ID";

        // 在运行服务对象中指定具体任务的变量, 设置单个值和多个值
        taskService.setVariable(taskId, "evection", evection);
        taskService.setVariables(taskId, processVarMap);
    }


}
